package com.example.administrator.myweiyue.ui.activity;


import android.content.res.ColorStateList;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.administrator.myweiyue.R;
import com.example.administrator.myweiyue.ui.base.BaseActivity;
import com.example.administrator.myweiyue.ui.component.AppComponent;
import com.example.administrator.myweiyue.ui.fragment.MyFragment;
import com.example.administrator.myweiyue.ui.fragment.OmeletteFragment;
import com.example.administrator.myweiyue.ui.fragment.news.NewFragment;
import com.example.administrator.myweiyue.ui.fragment.video.VideoFragment;

import java.util.ArrayList;

import static android.R.id.tabhost;

public class MainActivity extends BaseActivity {
    private String[] name;
    private ArrayList<Class<?>> classes;
    private FragmentTabHost mTabHost;
    private int[] drawableSelector;
    private ColorStateList mColorStateList;


    private void inittab() {
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        for (int i = 0; i < 4; i++) {
//        每个都设置一遍
            //newTabSper传入的参数是String类型的，作用是标签。
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(name[i]);
            View indicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
            ImageView imageView = (ImageView) indicator.findViewById(R.id.ic_icon);
            TextView textView = (TextView) indicator.findViewById(R.id.tv_tab);
            //设置图片选择器
            imageView.setBackgroundResource(drawableSelector[i]);
            //设置字体选择器
            if (mColorStateList == null) {
                mColorStateList = getResources().
                        getColorStateList(R.color.main_bottom_text_selector);
            }
            textView.setTextColor(mColorStateList);
            textView.setText(name[i]);
//把view设置给他
            tabSpec.setIndicator(indicator);
//第一个参数为穿入的标签，第二个为每个fragment的名字.class
            mTabHost.addTab(tabSpec, classes.get(i), null);
        }
    }

    private void initdata() {
        drawableSelector = new int[]{R.drawable.ic_news_selector, R.drawable.ic_video_selector, R.drawable.ic_jiandan_selector, R.drawable.ic_my_selector};
        name = new String[]{"新闻", "视频", "煎蛋", "我的"};
        classes = new ArrayList<>();
        classes.add(NewFragment.class);
        classes.add(VideoFragment.class);
        classes.add(OmeletteFragment.class);
        classes.add(MyFragment.class);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initInjector(AppComponent appComponent) {
//        DaggerComponent.builder().moudlue(new Moudlue(this, "钟洪文")).build().inject(this);
////        DaggerAppComponent.builder().
    }

    @Override
    public void afterViewCreate(View mRootView) {
        mTabHost = (FragmentTabHost) findViewById(tabhost);
        initdata();
        inittab();
        mTabHost.setCurrentTab(0);
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                //此id就是自己设置的标签
//                Gson gson = mAppComponent.gson();
//                LogUtils.debug("---------------"+gson.toString());
//                Retrofit retrofit = mAppComponent.getRetrofit();
//                LogUtils.debug("---------------"+retrofit);

            }
        });
    }

    @Override
    public void initData() {

    }


    @Override
    public void onRetry() {

    }
}
